var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var WxParse = require('../../utils/wxParse/wxParse.js');
var app = getApp();

Page({
  data: {
    pageJson: {},
  },
  onLoad() {
    var _self = this;

    this._modMapping = app.config.modMapping;

    this._initApp(function() {

      _self._getPageJson();
      _self._getIndexData();
      _self._quietLogin();
    });
  },

  //初始化配置信息
  _initApp(cb) {
    wx.getExtConfig({
      success: function(res) {
        var {
          host,
          alias
        } = res.extConfig;

        if (host) {
          app.setHost(host);
        }
        if (alias) {
          app.setAlias(alias);
        }
        cb && cb();
      }
    });
  },

  /**
   * 获取pageJson
   */
  _getPageJson() {
    var _self = this;

    ajax.G('/openapi/CPages/getHome', {}, function(res) {

      var data = res.result;
      var pageJson = JSON.parse(data['settings']);
      var pModules = _self._filterData(pageJson.pModules);

      pModules.forEach(function(mod) {
        if (mod.name === 'wxParse') {
          WxParse.wxParse('wxParseArticle', 'html', mod.content.html, _self, 0);
        }
      });

      _self.setData({
        pageJson,
      });
    });
  },

  //获取首页数据
  _getIndexData() {
    var _self = this;

    ajax.P('/openapi/CHome/index', {}, function(result) {
      var d = result.result;

      _self.setData({
        shop: {
          name: d.title,
          phone: d.contact,
          address: d.address,
          time: d.hours,
          logo: d.logo,
          shareTitle: d.share_title,
          kfState: d.kf_state
        },
        bannerList: d.bannerList,
        bannerListLayout: d.bannerListLayout,
        techList: d.technician,
        homeCatSetting: d.homeCatSetting,
        goodsModuleList: d.goodsModuleList,
        dataLoaded: true,

        hasNearShop: d.hasNearShop == 1
      });
      
      app.setShop(_self.data.shop);
    })
  },

  _filterData(pModules) {
    if (!pModules) {
      return;
    }
    var _self = this;

    pModules.forEach(function(mod) {
      var style = mod.content.style;
      var styleStr = '';

      Object.keys(style).forEach(function(key) {
        var val = style[key];

        key = getStyleName(key);
        styleStr += key + ':' + val + ';';
      });
      mod['content']['oStyle'] = style;
      mod['content']['style'] = styleStr;
      mod['name'] = _self._modMapping[mod['content']['type']];
    });
    return pModules;

    function getStyleName(name) {
      return name.replace(/([A-Z])/g, "-$1").toLowerCase();
    }
  },


  /**
   * openid静默登陆
   */
  _quietLogin: function() {
    var $this = this;

    wx.login({
      success: function(res) {
        ajax.G(
          '/openapi/CHome/getUserOpenID', {
            jscode: res.code
          },
          function(d) {
            app.setOpenId(d.result.openid);
            $this._openIdLogin(d.result.openid);
          }
        );
      },
      fail: function() {},
      complete: function() {}
    })
  },

  /**
   * openid静默登陆
   */
  _openIdLogin: function(openid) {
    var _self = this;

    ajax.P(
      '/openapi/CHome/login', {
        openId: openid
      },
      function(d) {
        var userSid = d.result.userSid;
        if (userSid) {
          app.setUserSid(userSid);
        }
      }
    );
  },

});